package com.tiyende.kash.loop.ui.presenter

import com.tiyende.kash.loop.common.CommonPresenter
import com.tiyende.kash.loop.request.HttpHelper
import com.tiyende.kash.loop.request.IHttpCallback
import com.tiyende.kash.loop.ui.contract.IViewContract
import com.tiyende.kash.loop.utils.log.TiyendeLogger
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/20 18:45
 * @Annotation：上传图片
 */
class TiyendeImageUploadPresenter(private val iView: IViewContract.ITiyendeImageUploadView): CommonPresenter(iView) {

    /**
     * 上传图片
     */
    fun submitUploadImageAction(imgPath: String){
        HttpHelper.upload(imgPath, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                TiyendeLogger.e("upload success: $data")
                iView.hiddenTiyendeLoading()
                // 从返回数据中获取图片URL
                val imgUrl = data.optString("imageUrl")
                if (imgUrl.isNotEmpty()) {
                    iView.showTiyendeImageUploadSuccess(imgPath, imgUrl)
                } else {
                    // 兼容旧格式
                    val obj = data.optJSONObject("obj")
                    obj?.let {
                        val url = it.optString("url")
                        iView.showTiyendeImageUploadSuccess(imgPath, url)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                TiyendeLogger.e("upload error: $error")
                iView.hiddenTiyendeLoading()
                val errorMsg = error.optString("errorMsg", "上传失败")
                iView.showTiyendeToast(errorMsg)
            }

            override fun onRequestFinish() {
                TiyendeLogger.e("upload finish")
                iView.hiddenTiyendeLoading()
            }
        })
//        HttpHelper.upload(imgPath,object : StringCallback(){
//            override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
//                super.onStart(request)
//                TiyendeLogger.e("upload start")
//            }
//            override fun onSuccess(response: Response<String>?) {
//                TiyendeLogger.e("upload success ："+response?.body())
//                if (response?.code() == 200){
//                    iView.hiddenTiyendeLoading()
//                    val data = JSONObject(response.body())
//                    when(data.optInt("code")){
//                        0 -> {//成功
//                            val obj = data.optJSONObject("obj")
//                            obj?.let {
//                                val imgUrl = it.optString("url")
//                                iView.showTiyendeImageUploadSuccess(imgPath,imgUrl)
//                            }
//                        }
//                        else -> {
//                            val msg = data.optString("msg")
//                            iView.showTiyendeToast(msg)
//                        }
//                    }
//                } else {
//                    iView.showTiyendeToast(response?.message())
//                }
//            }
//            override fun onError(response: Response<String>?) {
//                TiyendeLogger.e("upload error，code： ${response?.code()} , message：${response?.message()}")
//                response?.let {
//                    try {
//                        val responseBody = it.rawResponse.body
//                        val bytes = IOUtils.toByteArray(responseBody?.byteStream())
//                        val body = String(bytes, Charset.forName("UTF-8"))
//                        TiyendeLogger.e("upload error，code： $body")
//                        val msg = JSONObject(body).optString("msg")
//                        iView.showTiyendeToast(msg)
//                    } catch (e: Exception){
//                        iView.showTiyendeToast(response.message())
//                    }
//                }
//            }
//            override fun uploadProgress(progress: Progress) {
//                TiyendeLogger.e("uploadProgress: $progress")
//                TiyendeLogger.e("uploadProgress，total: ${progress.totalSize}")
//                TiyendeLogger.e("uploadProgress，progress: ${progress.currentSize}")
//            }
//            override fun onFinish() {
//                iView.hiddenTiyendeLoading()
//            }
//        })
    }
}